
import { Component, OnInit, ViewChild } from '@angular/core';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Rx';
import { CommService } from '../../comm/comm.service';
import { ReportUrls } from '../../comm/report.urls.conf';
import { SubArea } from '../../model/report/sub-area';
import { QueryModel } from '../../model/report/query-model';
declare const $: any;
import * as Enumerable from 'linq';
import { TVResource } from 'app/ptpmc/model/comm/tv-resource';
import { DxDropDownBoxComponent } from 'devextreme-angular';
import { UserModel } from '../../model/sys/user-model';
import { Notifycation } from '../../comm/notifycation';

@Component({
    selector: 'app-report-ctdd',
    templateUrl: './phcntd.component.html',
    styleUrls: ['./phcntd.component.scss']
})

export class PhCNTDComponent implements OnInit {
    title = '产量OEE';
    public templateItems: any[] = [];
    public templateSel;
    public list = new Array();
    public chartOption1: any;
    public chartOption2: any;
    public chartOption3: any;
    showloading = true;
    /*生产线 - 班次*/
    public subAreaItems: SubArea[];
    public subAreaSelected: any[];
    public shiftItems: any[];
    public shiftSelected: any;
    public equipemntItems: TVResource[] = [];
    public equipmentSelected: TVResource[];

    public isShowCharts1 = true;
    public isShowCharts2 = false;
    public isShowCharts3 = false;

    public allRows = [];
    public items: any[];

    typeList: any = [
        { id: 1, name: '按日期明细' },
        { id: 2, name: '按班次平均' },
    ];

    type = 1;

    public workNum1 = 0;
    public workNum2 = 0;
    public workNum3 = 0;

    private blockList = [];

    private blockIndex = 1;

    @ViewChild('dddb') dddb: DxDropDownBoxComponent;
    @ViewChild('ddda') ddda: DxDropDownBoxComponent;

    constructor(private commServices: CommService) {
    }

    ngOnInit() {
        this.commServices.initDatePicker($('#DateTimeRange'), 'YYYY-MM-DD');
        this.shiftItems = this.commServices.shifts;
        this.shiftSelected = this.shiftItems[0];
        this.subAreaItems = this.commServices.getSubAreas();
        this.subAreaSelected = [];
        this.equipemntItems = [];
        this.subAreaSelected.forEach(e => {
            const arr = this.commServices.getTVResourceBySubArea(e.subArea);
            arr.forEach(a => { this.equipemntItems.push(a); });
        });

        this.equipmentSelected = [];

        this.initTemplate();

    }

    saveChanges() {
        // const updateArr = this.blockList.filter(e => e.faultReason !== '' 
        // || e.faultStation !== '' || e.wxFx !== '' || e.zlFx !== '');

        this.blockList.forEach(faultOee => {

            this.commServices.doPost('/prodCount/analyse/update',
                {
                    faultId: faultOee.faultId, faultType: faultOee.blockStatus, faultStation: faultOee.faultStation,
                    faultReason: faultOee.faultReason, wxFx: faultOee.wxFx, zlFx: faultOee.zlFx,
                    updateUser: UserModel.userName,
                    faultEquipment: faultOee.station, faultTime: faultOee.endTime, faultDuration: faultOee.duration
                }
            ).subscribe(e => {
                // Notifycation.showNotification(e.resultCode === 200 ? '修改成功' : '修改失败', 'info');
            });
        });
        Notifycation.showNotification('修改完成', 'info');
    }
    clearEquipmentSelected() {
        this.equipmentSelected = [];
    }
    equipmentSelectedOk() {
        this.dddb.instance.close();
    }
    clearSubAreaSelected() {
        this.subAreaSelected = [];
    }
    subAreaSelectedOk() {
        this.ddda.instance.close();
    }

    initTemplate() {
        this.templateItems = [];
        this.templateSel = {};
        this.commServices
            .doGet(ReportUrls.ProdHourTemplateAreas2 + '/' + this.subAreaSelected[0].subArea)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.templateItems = model.data;
                    this.templateSel = this.templateItems[0];
                } else {
                    alert(model.resultMessage);
                }
            });
    }

    onSubAreaSelectedChange(subArea: any): void {
        this.equipemntItems = [];
        this.subAreaSelected.forEach(e => {
            const arr = this.commServices.getTVResourceBySubArea(e.subArea);
            arr.forEach(a => { this.equipemntItems.push(a); });
        });

        this.equipmentSelected = [];

        this.initTemplate();
    }

    dataMap = new Map<String, any>();

    workHourMap = new Map<string, number>();

    getBlockList(type: string) {
        const queryModel: QueryModel = new QueryModel();
        queryModel.dateTimeRange = this.getTime('DateTimeRange');
        queryModel.querySubAreaIds = this.subAreaSelected.map(e => e.subArea);
        if (type === 'checked') {
            this.blockIndex = 1;
            queryModel.queryResourceIds = this.equipmentSelected.map(e => e.resource);
        } else {
            this.blockIndex = 0;
            queryModel.queryResourceIds = this.equipemntItems.map(e => e.resource);
        }
        queryModel.shift = this.shiftSelected.shiftId;
        queryModel.tag = this.templateSel.templateName;
        this.commServices
            .getList(queryModel, ReportUrls.PtrCntOeeBlock)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.blockList = model.data;
                } else {
                    alert(model.resultMessage);
                }
            });
    }

    getReportData(): void {
        const queryModel: QueryModel = new QueryModel();
        queryModel.dateTimeRange = this.getTime('DateTimeRange');
        queryModel.querySubAreaIds = this.subAreaSelected.map(e => e.subArea);
        queryModel.queryResourceIds = this.equipmentSelected.map(e => e.stationCode);
        queryModel.shift = this.shiftSelected.shiftId;
        queryModel.tag = this.templateSel.templateName;

        this.list = [];
        this.isShowCharts1 = false;
        this.isShowCharts2 = false;
        this.isShowCharts3 = false;

        this.commServices
            .getList(queryModel, ReportUrls.PtrCnt2)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.colorMap.clear();
                    let datas: any[] = model.data.oee;
                    let block: any[] = model.data.block;
                    block = Enumerable.from((block)).orderBy(e => e.endTime).toArray();
                    block.forEach(item => {
                        const end = new Date(item.endTime).getTime();
                        const start = new Date().getTime();
                        const v = start - end;
                        if (v / (1000 * 60 * 60 * 24) <= 3) {
                            item.editEnable = true;
                        } else {
                            item.editEnable = false;
                        }
                        if(queryModel.querySubAreaIds[0].slice(0,2) === 'GF'){
                            var sub: string = '';
                            sub = item.subArea.replace(/(^\s*)|(\s*$)/g, "");
                            switch(sub){
                                case 'GFAL1': sub += '_装配主线';  break;
                                case 'GFBU1': sub += '_测试线';  break;
                                case 'GFCL1': sub += '_CASE 机加工线';  break;
                                case 'GFCR1': sub += '_行星齿轮组分装线';  break;
                                case 'GFCS1': sub += '_中间支撑架分装线';  break;
                                case 'GFDH1': sub += '_飞轮盘减震器壳体分装线';  break;
                                case 'GFHL1': sub += '_HOUSING 机加工线';  break;
                                case 'GFTC1': sub += '_壳体分装线';  break;
                                case 'GFTD1': sub += '_中间轴&差速器分装线';  break;
                            }
                            item.subArea = sub;
                        }
                    });
                    this.blockList = block;
                    datas = Enumerable.from((datas)).orderBy(e => e.stationCode)
                        .orderBy(e => e.shiftVal0)
                        .orderBy(e => e.prodDateVal0).toArray();

                    // 将 同一个班次 同一个工位的数据平均
                    if (this.type === 2) {
                        const dataMap = new Map<string, Array<any>>();
                        datas.forEach(e => {
                            if (!dataMap.has(e.stationCode + e.shiftVal0)) {
                                dataMap.set(e.stationCode + e.shiftVal0, new Array());
                            }
                            const arr = dataMap.get(e.stationCode + e.shiftVal0);
                            arr.push(e);
                            dataMap.set(e.stationCode + e.shiftVal0, arr);
                        });

                        const resultArr = new Array();
                        dataMap.forEach((v, k) => {
                            const resultBean = v[0];
                            let sum1 = 0;
                            let sum2 = 0;
                            let sum3 = 0;
                            let sum4 = 0;
                            let sum5 = 0;
                            let sum6 = 0;
                            let sum7 = 0;
                            let sum8 = 0;
                            let sum9 = 0;
                            let sum10 = 0;
                            let sum11 = 0;

                            let sumr1 = 0;
                            let sumr2 = 0;
                            let sumr3 = 0;
                            let sumr4 = 0;
                            let sumr5 = 0;
                            let sumr6 = 0;
                            let sumr7 = 0;
                            let sumr8 = 0;
                            let sumr9 = 0;
                            let sumr10 = 0;
                            let sumr11 = 0;
                            v.forEach(e => {
                                // 实际产量
                                const h1 = e.hourVal1;
                                const h2 = e.hourVal2;
                                const h3 = e.hourVal3;
                                const h4 = e.hourVal4;
                                const h5 = e.hourVal5;
                                const h6 = e.hourVal6;
                                const h7 = e.hourVal7;
                                const h8 = e.hourVal8;
                                const h9 = e.hourVal9;
                                const h10 = e.hourVal10;
                                const h11 = e.hourVal11;
                                // 吃饭产量
                                const hr1 = e.hourVal1Rest;
                                const hr2 = e.hourVal2Rest;
                                const hr3 = e.hourVal3Rest;
                                const hr4 = e.hourVal4Rest;
                                const hr5 = e.hourVal5Rest;
                                const hr6 = e.hourVal6Rest;
                                const hr7 = e.hourVal7Rest;
                                const hr8 = e.hourVal8Rest;
                                const hr9 = e.hourVal9Rest;
                                const hr10 = e.hourVal10Rest;
                                const hr11 = e.hourVal11Rest;

                                sum1 += h1; sum2 += h2; sum3 += h3; sum4 += h4; sum5 += h5; sum6 += h6;
                                sum7 += h7; sum8 += h8; sum9 += h9; sum10 += h10; sum11 += h11;

                                sumr1 += hr1; sumr2 += hr2; sumr3 += hr3; sumr4 += hr4; sumr5 += hr5; sumr6 += hr6;
                                sumr7 += hr7; sumr8 += hr8; sumr9 += hr9; sumr10 += hr10; sumr11 += hr11;

                            });
                            resultBean.hourVal1 = (sum1 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);
                            resultBean.hourVal2 = (sum2 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);
                            resultBean.hourVal3 = (sum3 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);
                            resultBean.hourVal4 = (sum4 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);
                            resultBean.hourVal5 = (sum5 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);
                            resultBean.hourVal6 = (sum6 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);
                            resultBean.hourVal7 = (sum7 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);
                            resultBean.hourVal8 = (sum8 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);
                            resultBean.hourVal9 = (sum9 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);
                            resultBean.hourVal10 = (sum10 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);
                            resultBean.hourVal11 = (sum11 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);

                            resultBean.hourVal1Rest =
                                (sumr1 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);
                            resultBean.hourVal2Rest =
                                (sumr2 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);
                            resultBean.hourVal3Rest =
                                (sumr3 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);
                            resultBean.hourVal4Rest =
                                (sumr4 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);
                            resultBean.hourVal5Rest =
                                (sumr5 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);
                            resultBean.hourVal6Rest =
                                (sumr6 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);
                            resultBean.hourVal7Rest =
                                (sumr7 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);
                            resultBean.hourVal8Rest =
                                (sumr8 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);
                            resultBean.hourVal9Rest =
                                (sumr9 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);
                            resultBean.hourVal10Rest =
                                (sumr10 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);
                            resultBean.hourVal11Rest =
                                (sumr11 / dataMap.get(resultBean.stationCode + resultBean.shiftVal0).length);

                            resultBean.prodDateVal0 = '';

                            resultBean.hourVal1 = (resultBean.hourVal1 === undefined ? 0 : resultBean.hourVal1.toFixed(1));
                            resultBean.hourVal2 = (resultBean.hourVal2 === undefined ? 0 : resultBean.hourVal2.toFixed(1));
                            resultBean.hourVal3 = (resultBean.hourVal3 === undefined ? 0 : resultBean.hourVal3.toFixed(1));
                            resultBean.hourVal4 = (resultBean.hourVal4 === undefined ? 0 : resultBean.hourVal4.toFixed(1));
                            resultBean.hourVal5 = (resultBean.hourVal5 === undefined ? 0 : resultBean.hourVal5.toFixed(1));
                            resultBean.hourVal6 = (resultBean.hourVal6 === undefined ? 0 : resultBean.hourVal6.toFixed(1));
                            resultBean.hourVal7 = (resultBean.hourVal7 === undefined ? 0 : resultBean.hourVal7.toFixed(1));
                            resultBean.hourVal8 = (resultBean.hourVal8 === undefined ? 0 : resultBean.hourVal8.toFixed(1));
                            resultBean.hourVal9 = (resultBean.hourVal9 === undefined ? 0 : resultBean.hourVal9.toFixed(1));
                            resultBean.hourVal10 = (resultBean.hourVal10 === undefined ? 0 : resultBean.hourVal10.toFixed(1));
                            resultBean.hourVal11 = (resultBean.hourVal11 === undefined ? 0 : resultBean.hourVal11.toFixed(1));

                            resultBean.hourVal1Rest = (resultBean.hourVal1Rest === undefined ? 0 : resultBean.hourVal1Rest.toFixed(1));
                            resultBean.hourVal2Rest = (resultBean.hourVal2Rest === undefined ? 0 : resultBean.hourVal2Rest.toFixed(1));
                            resultBean.hourVal3Rest = (resultBean.hourVal3Rest === undefined ? 0 : resultBean.hourVal3Rest.toFixed(1));
                            resultBean.hourVal4Rest = (resultBean.hourVal4Rest === undefined ? 0 : resultBean.hourVal4Rest.toFixed(1));
                            resultBean.hourVal5Rest = (resultBean.hourVal5Rest === undefined ? 0 : resultBean.hourVal5Rest.toFixed(1));
                            resultBean.hourVal6Rest = (resultBean.hourVal6Rest === undefined ? 0 : resultBean.hourVal6Rest.toFixed(1));
                            resultBean.hourVal7Rest = (resultBean.hourVal7Rest === undefined ? 0 : resultBean.hourVal7Rest.toFixed(1));
                            resultBean.hourVal8Rest = (resultBean.hourVal8Rest === undefined ? 0 : resultBean.hourVal8Rest.toFixed(1));
                            resultBean.hourVal9Rest = (resultBean.hourVal9Rest === undefined ? 0 : resultBean.hourVal9Rest.toFixed(1));
                            resultBean.hourVal10Rest = (resultBean.hourVal10Rest === undefined ? 0 : resultBean.hourVal10Rest.toFixed(1));
                            resultBean.hourVal11Rest = (resultBean.hourVal11Rest === undefined ? 0 : resultBean.hourVal11Rest.toFixed(1));

                            resultArr.push(resultBean);

                        });

                        datas = resultArr;

                    }

                    datas.forEach(e => {
                        this.workHourMap.set(e.shiftVal0 + '班', e.templateWorkday);

                        this.list.push(this.genTableList(e, queryModel.querySubAreaIds[0] === undefined ? ''
                            : queryModel.querySubAreaIds[0], queryModel.queryResourceIds[0] === undefined ? ''
                                : this.equipemntItems.find(el => el.stationCode === queryModel.queryResourceIds[0]).equipment));
                    });

                    // 计算一天的数据的jph oee平均值
                    this.list.forEach(e => {
                        if (!this.dataMap.has(e[0].c3 + e[0].c4 + 'jph')) {
                            this.dataMap.set(e[0].c3 + e[0].c4 + 'jph', 0);
                            this.dataMap.set(e[0].c3 + e[0].c4 + 'oee', 0);
                        }
                        let jphCount = this.dataMap.get(e[0].c3 + e[0].c4 + 'jph');
                        let oeeCount = this.dataMap.get(e[0].c3 + e[0].c4 + 'oee');

                        const workHour = this.workHourMap.get(e[0].c2);

                        if (workHour >= 1) { jphCount += e[2].c2 === undefined ? 0 : e[2].c2; };
                        if (workHour >= 2) { jphCount += e[2].c3 === undefined ? 0 : e[2].c3; };
                        if (workHour >= 3) { jphCount += e[2].c4 === undefined ? 0 : e[2].c4; };
                        if (workHour >= 4) { jphCount += e[2].c5 === undefined ? 0 : e[2].c5; };
                        if (workHour >= 5) { jphCount += e[2].c6 === undefined ? 0 : e[2].c6; };
                        if (workHour >= 6) { jphCount += e[2].c7 === undefined ? 0 : e[2].c7; };
                        if (workHour >= 7) { jphCount += e[2].c8 === undefined ? 0 : e[2].c8; };
                        if (workHour >= 8) { jphCount += e[2].c9 === undefined ? 0 : e[2].c9; };
                        if (workHour >= 9) { jphCount += e[2].c10 === undefined ? 0 : e[2].c10; };
                        if (workHour >= 10) { jphCount += e[2].c11 === undefined ? 0 : e[2].c11; };
                        if (workHour >= 11) { jphCount += e[2].c12 === undefined ? 0 : e[2].c12; };

                        if (workHour >= 1) { oeeCount += e[6].c2 === undefined ? 0 : e[6].c2; };
                        if (workHour >= 2) { oeeCount += e[6].c3 === undefined ? 0 : e[6].c3; };
                        if (workHour >= 3) { oeeCount += e[6].c4 === undefined ? 0 : e[6].c4; };
                        if (workHour >= 4) { oeeCount += e[6].c5 === undefined ? 0 : e[6].c5; };
                        if (workHour >= 5) { oeeCount += e[6].c6 === undefined ? 0 : e[6].c6; };
                        if (workHour >= 6) { oeeCount += e[6].c7 === undefined ? 0 : e[6].c7; };
                        if (workHour >= 7) { oeeCount += e[6].c8 === undefined ? 0 : e[6].c8; };
                        if (workHour >= 8) { oeeCount += e[6].c9 === undefined ? 0 : e[6].c9; };
                        if (workHour >= 9) { oeeCount += e[6].c10 === undefined ? 0 : e[6].c10; };
                        if (workHour >= 10) { oeeCount += e[6].c11 === undefined ? 0 : e[6].c11; };
                        if (workHour >= 11) { oeeCount += e[6].c12 === undefined ? 0 : e[6].c12; };

                        this.dataMap.set(e[0].c3 + e[0].c4 + 'jph', jphCount);
                        this.dataMap.set(e[0].c3 + e[0].c4 + 'oee', oeeCount);
                    });

                    this.list.forEach(e => {
                        const jphCount = this.dataMap.get(e[0].c3 + e[0].c4 + 'jph');
                        e[0].c8 = (jphCount / datas[0].templateAllWorkday) > 100 ? 100
                            : (jphCount / datas[0].templateAllWorkday).toFixed(1);

                        const oeeCount = this.dataMap.get(e[0].c3 + e[0].c4 + 'oee');
                        e[0].c6 = (oeeCount / datas[0].templateAllWorkday) > 100 ? 100
                            : ((oeeCount / datas[0].templateAllWorkday).toFixed(1));
                    });

                    this.items = datas;

                    this.showCharts();

                    // 列表显示数据
                    this.allRows = [];
                    const dayList = new Array();
                    for (let i = 0; i < this.list.length; i++) {
                        const e = this.list[i];
                        e.forEach(element => {
                            this.allRows.push(element);
                        });
                        if (dayList.find(el => el === e[0].c4) === undefined) {
                            if (i + 1 < this.list.length) {
                                if (e[0].c4 + e[0].c3 + e[0].c2
                                    !== this.list[i + 1][0].c4 + this.list[i + 1][0].c3 + this.list[i + 1][0].c2) {
                                    this.allRows.push(this.spaceRow());
                                    dayList.push(e[0].c4 + e[0].c3 + e[0].c2);
                                }
                            }
                        }
                    }

                } else {
                    alert(model.resultMessage);
                }
            });
    }

    getTime(id: any): String {
        return $('#' + id).val();
    }

    spaceRow() {
        return {
            c1: '',
            c2: '',
            c3: '',
            c4: '',
            c5: '',
            c6: '',
            c7: '',
            c8: '',
            c9: '',
            c10: '',
            c11: '',
            c12: ''
        };
    }

    genTableList(datas: any, subArea: any, equipment: any) {
        const array = new Array();
        for (let i = 0; i < 8; i++) {
            if (i === 0) {
                const tableBean = {
                    c1: subArea,
                    c2: datas === undefined ? '' : datas
                        .shiftVal0 + '班',
                    c3: datas.stationCode,
                    c4: datas.prodDateVal0,
                    c5: '每天平均OEE',
                    c6: '',
                    c7: '每天平均JPH',
                    c8: '',
                    c9: '',
                    c10: '',
                    c11: '',
                    c12: ''
                };
                array.push(tableBean);
            } else if (i === 1) {
                const tableBean = {
                    c1: '小时数',
                    c2: '1',
                    c3: '2',
                    c4: '3',
                    c5: '4',
                    c6: '5',
                    c7: '6',
                    c8: '7',
                    c9: '8',
                    c10: '9',
                    c11: '10',
                    c12: '11'
                };
                array.push(tableBean);
            } else if (i === 2) {
                const tableBean = {
                    c1: '产量',
                    c2: this.getRealProd(datas.hourVal1, datas.hourVal1Rest),
                    c3: this.getRealProd(datas.hourVal2, datas.hourVal2Rest),
                    c4: this.getRealProd(datas.hourVal3, datas.hourVal3Rest),
                    c5: this.getRealProd(datas.hourVal4, datas.hourVal4Rest),
                    c6: this.getRealProd(datas.hourVal5, datas.hourVal5Rest),
                    c7: this.getRealProd(datas.hourVal6, datas.hourVal6Rest),
                    c8: this.getRealProd(datas.hourVal7, datas.hourVal7Rest),
                    c9: this.getRealProd(datas.hourVal8, datas.hourVal8Rest),
                    c10: this.getRealProd(datas.hourVal9, datas.hourVal9Rest),
                    c11: this.getRealProd(datas.hourVal10, datas.hourVal10Rest),
                    c12: this.getRealProd(datas.hourVal11, datas.hourVal11Rest),
                };
                array.push(tableBean);
            } else if (i === 3) {
                const tableBean = {
                    c1: 'JPH',
                    c2: datas.hourVal1Jph,
                    c3: datas.hourVal2Jph,
                    c4: datas.hourVal3Jph,
                    c5: datas.hourVal4Jph,
                    c6: datas.hourVal5Jph,
                    c7: datas.hourVal6Jph,
                    c8: datas.hourVal7Jph,
                    c9: datas.hourVal8Jph,
                    c10: datas.hourVal9Jph,
                    c11: datas.hourVal10Jph,
                    c12: datas.hourVal11Jph,
                };
                array.push(tableBean);
            } else if (i === 4) {
                const tableBean = {
                    c1: '设备利用率',
                    c2: this.getOEE(datas.hourVal1 - datas.hourVal1Rest, datas.hourVal1Jph) + '%',
                    c3: this.getOEE(datas.hourVal2 - datas.hourVal2Rest, datas.hourVal2Jph) + '%',
                    c4: this.getOEE(datas.hourVal3 - datas.hourVal3Rest, datas.hourVal3Jph) + '%',
                    c5: this.getOEE(datas.hourVal4 - datas.hourVal4Rest, datas.hourVal4Jph) + '%',
                    c6: this.getOEE(datas.hourVal5 - datas.hourVal5Rest, datas.hourVal5Jph) + '%',
                    c7: this.getOEE(datas.hourVal6 - datas.hourVal6Rest, datas.hourVal6Jph) + '%',
                    c8: this.getOEE(datas.hourVal7 - datas.hourVal7Rest, datas.hourVal7Jph) + '%',
                    c9: this.getOEE(datas.hourVal8 - datas.hourVal8Rest, datas.hourVal8Jph) + '%',
                    c10: this.getOEE(datas.hourVal9 - datas.hourVal9Rest, datas.hourVal9Jph) + '%',
                    c11: this.getOEE(datas.hourVal10 - datas.hourVal10Rest, datas.hourVal10Jph) + '%',
                    c12: this.getOEE(datas.hourVal11 - datas.hourVal11Rest, datas.hourVal11Jph) + '%'
                };
                array.push(tableBean);
            } else if (i === 5) {
                const tableBean = {
                    c1: '吃饭/休息扣除',
                    c2: datas.hourVal1Rest,
                    c3: datas.hourVal2Rest,
                    c4: datas.hourVal3Rest,
                    c5: datas.hourVal4Rest,
                    c6: datas.hourVal5Rest,
                    c7: datas.hourVal6Rest,
                    c8: datas.hourVal7Rest,
                    c9: datas.hourVal8Rest,
                    c10: datas.hourVal9Rest,
                    c11: datas.hourVal10Rest,
                    c12: datas.hourVal11Rest,
                };
                array.push(tableBean);
            } else if (i === 6) {
                const tableBean = {
                    c1: 'OEE',
                    c2: this.getOEE(datas.hourVal1 - datas.hourVal1Rest, datas.hourVal1Jph),
                    c3: this.getOEE(datas.hourVal2 - datas.hourVal2Rest, datas.hourVal2Jph),
                    c4: this.getOEE(datas.hourVal3 - datas.hourVal3Rest, datas.hourVal3Jph),
                    c5: this.getOEE(datas.hourVal4 - datas.hourVal4Rest, datas.hourVal4Jph),
                    c6: this.getOEE(datas.hourVal5 - datas.hourVal5Rest, datas.hourVal5Jph),
                    c7: this.getOEE(datas.hourVal6 - datas.hourVal6Rest, datas.hourVal6Jph),
                    c8: this.getOEE(datas.hourVal7 - datas.hourVal7Rest, datas.hourVal7Jph),
                    c9: this.getOEE(datas.hourVal8 - datas.hourVal8Rest, datas.hourVal8Jph),
                    c10: this.getOEE(datas.hourVal9 - datas.hourVal9Rest, datas.hourVal9Jph),
                    c11: this.getOEE(datas.hourVal10 - datas.hourVal10Rest, datas.hourVal10Jph),
                    c12: this.getOEE(datas.hourVal11 - datas.hourVal11Rest, datas.hourVal11Jph)
                };
                array.push(tableBean);
            } else if (i === 7) {
                const tableBean = {
                    c1: '平均JPH',
                    c2: this.getAvgJph(datas),
                    c3: '平均OEE',
                    c4: this.getAvgOEE(datas) + '%',
                    c5: '',
                    c6: '',
                    c7: '',
                    c8: '',
                    c9: '',
                    c10: '',
                    c11: '',
                    c12: ''
                };
                array.push(tableBean);
            }
        }
        return array;
    }

    getOEE(prodCout: number, jph: number): number {
        return ((prodCout === undefined || jph === undefined || jph === 0 || prodCout === 0) ? 0
            : prodCout / jph > 1 ? 100 : Number.parseFloat((prodCout / jph * 100).toFixed(1)));
    }

    getAvgOEE(datas: any) {
        if (datas === undefined) { return 0; }
        let sum = 0;
        const workHour = this.workHourMap.get(datas.shiftVal0 + '班');
        if (workHour >= 1) { sum += this.getOEE(datas.hourVal1 - datas.hourVal1Rest, datas.hourVal1Jph); }
        if (workHour >= 2) { sum += this.getOEE(datas.hourVal2 - datas.hourVal2Rest, datas.hourVal2Jph); }
        if (workHour >= 3) { sum += this.getOEE(datas.hourVal3 - datas.hourVal3Rest, datas.hourVal3Jph); }
        if (workHour >= 4) { sum += this.getOEE(datas.hourVal4 - datas.hourVal4Rest, datas.hourVal4Jph); }
        if (workHour >= 5) { sum += this.getOEE(datas.hourVal5 - datas.hourVal5Rest, datas.hourVal5Jph); }
        if (workHour >= 6) { sum += this.getOEE(datas.hourVal6 - datas.hourVal6Rest, datas.hourVal6Jph); }
        if (workHour >= 7) { sum += this.getOEE(datas.hourVal7 - datas.hourVal7Rest, datas.hourVal7Jph); }
        if (workHour >= 8) { sum += this.getOEE(datas.hourVal8 - datas.hourVal8Rest, datas.hourVal8Jph); }
        if (workHour >= 9) { sum += this.getOEE(datas.hourVal9 - datas.hourVal9Rest, datas.hourVal9Jph); }
        if (workHour >= 10) { sum += this.getOEE(datas.hourVal10 - datas.hourVal10Rest, datas.hourVal10Jph); }
        if (workHour >= 11) { sum += this.getOEE(datas.hourVal11 - datas.hourVal11Rest, datas.hourVal11Jph); }
        const oee = (sum / datas.templateWorkday).toFixed(1);
        return oee;
    }

    getAvgJph(datas: any) {
        if (datas === undefined) { return 0; }
        let sum = 0;
        const workHour = this.workHourMap.get(datas.shiftVal0 + '班');
        if (workHour >= 1) { sum += datas.hourVal1 - datas.hourVal1Rest; }
        if (workHour >= 2) { sum += datas.hourVal2 - datas.hourVal2Rest; }
        if (workHour >= 3) { sum += datas.hourVal3 - datas.hourVal3Rest; }
        if (workHour >= 4) { sum += datas.hourVal4 - datas.hourVal4Rest; }
        if (workHour >= 5) { sum += datas.hourVal5 - datas.hourVal5Rest; }
        if (workHour >= 6) { sum += datas.hourVal6 - datas.hourVal6Rest; }
        if (workHour >= 7) { sum += datas.hourVal7 - datas.hourVal7Rest; }
        if (workHour >= 8) { sum += datas.hourVal8 - datas.hourVal8Rest; }
        if (workHour >= 9) { sum += datas.hourVal9 - datas.hourVal9Rest; }
        if (workHour >= 10) { sum += datas.hourVal10 - datas.hourVal10Rest; }
        if (workHour >= 11) { sum += datas.hourVal11 - datas.hourVal11Rest; }

        if (datas.shiftVal0 === 1) {
            this.workNum1 = datas.templateWorkday;
        }
        if (datas.shiftVal0 === 2) {
            this.workNum2 = datas.templateWorkday;
        }
        if (datas.shiftVal0 === 3) {
            this.workNum3 = datas.templateWorkday;
        }

        const hourNum = datas.templateWorkday;

        return (sum / hourNum).toFixed(1);
    }


    getRealProd(prodCout: number, restprod: number) {
        if (prodCout === undefined) { return 0; }
        if (restprod === undefined) { restprod = 0; }
        return Number.parseFloat((prodCout - restprod).toFixed(1));
    }

    /**ECharts图表相关*/
    // tslint:disable-next-line:member-ordering
    chartsMap = new Map<String, any>();
    onChartInit(index: String, echartsIntance: any): void {
        this.chartsMap.set(index, echartsIntance);
        Observable.fromEvent(window, 'resize')
            .debounceTime(100) // 以免频繁处理
            .subscribe((event) => this.chartsMap.forEach((v, k) => v.resize()));
    }

    getGrossJph(shift: number): Array<number> {
        const datas = this.items.find(e => e.shiftVal0 === shift);
        const arr = new Array();
        arr.push(datas.hourVal1GrossJph);
        arr.push(datas.hourVal2GrossJph);
        arr.push(datas.hourVal3GrossJph);
        arr.push(datas.hourVal4GrossJph);
        arr.push(datas.hourVal5GrossJph);
        arr.push(datas.hourVal6GrossJph);
        arr.push(datas.hourVal7GrossJph);
        arr.push(datas.hourVal8GrossJph);
        arr.push(datas.hourVal9GrossJph);
        arr.push(datas.hourVal10GrossJph);
        arr.push(datas.hourVal11GrossJph);
        return arr;
    }

    showCharts() {

        const beas1 = new Array();
        const beas2 = new Array();
        const beas3 = new Array();

        const legend = new Array();
        legend.push('目标JPH');
        legend.push('GrossJPH');

        // 每天 每个班次
        this.list.forEach(e => {

            if (legend.find(el => (el + '/' + e[0].c3 + e[0].c2) === (e[0].c4 + e[0].c3 + '/' + e[0].c2)) === undefined) {
                legend.push(e[0].c4 + '_' + e[0].c3);
            }

            let a = new Array();

            if (e[0].c2 === '1班') {

                a.push(e[3].c2);
                a.push(e[3].c3);
                a.push(e[3].c4);
                a.push(e[3].c5);
                a.push(e[3].c6);
                a.push(e[3].c7);
                a.push(e[3].c8);
                a.push(e[3].c9);
                a.push(e[3].c10);
                a.push(e[3].c11);
                a.push(e[3].c12);
                beas1.push({
                    data: a, type: 'line', name: '目标JPH', label: {
                        normal: {
                            show: true,
                            position: 'top'
                        }
                    }, itemStyle: {
                        normal: { color: '#000' },
                    }
                }, {
                        data: this.getGrossJph(1), type: 'line', name: 'GrossJPH', label: {
                            normal: {
                                show: true,
                                position: 'top'
                            }
                        }, itemStyle: {
                            normal: { color: '#000' },
                        }
                    });

                const a1 = new Array();
                a1.push(e[5].c2);
                a1.push(e[5].c3);
                a1.push(e[5].c4);
                a1.push(e[5].c5);
                a1.push(e[5].c6);
                a1.push(e[5].c7);
                a1.push(e[5].c8);
                a1.push(e[5].c9);
                a1.push(e[5].c10);
                a1.push(e[5].c11);
                a1.push(e[5].c12);

                const color = this.getColor(e[0].c2);
                beas1.push({
                    data: a1, type: 'bar', name: e[0].c4 + '_' + e[0].c3 + '吃饭/休息', label: {
                        normal: {
                            show: true,
                            position: 'top'
                        }
                    }, itemStyle: {
                        normal: { color: color },
                    }
                });

                a = new Array();
                a.push(e[2].c2);
                a.push(e[2].c3);
                a.push(e[2].c4);
                a.push(e[2].c5);
                a.push(e[2].c6);
                a.push(e[2].c7);
                a.push(e[2].c8);
                a.push(e[2].c9);
                a.push(e[2].c10);
                a.push(e[2].c11);
                a.push(e[2].c12);
                beas1.push({
                    data: a, type: 'line', name: e[0].c4 + '_' + e[0].c3, label: {
                        normal: {
                            show: true,
                            position: 'top'
                        }
                    }, itemStyle: {
                        normal: { color: color },
                    }
                });
            } else if (e[0].c2 === '2班') {
                a = new Array();
                a.push(e[3].c2);
                a.push(e[3].c3);
                a.push(e[3].c4);
                a.push(e[3].c5);
                a.push(e[3].c6);
                a.push(e[3].c7);
                a.push(e[3].c8);
                a.push(e[3].c9);
                a.push(e[3].c10);
                a.push(e[3].c11);
                a.push(e[3].c12);
                beas2.push({
                    data: a, type: 'line', name: '目标JPH', label: {
                        normal: {
                            show: true,
                            position: 'top'
                        }
                    }, itemStyle: {
                        normal: { color: '#000' },
                    }
                }, {
                        data: this.getGrossJph(2), type: 'line', name: 'GrossJPH', label: {
                            normal: {
                                show: true,
                                position: 'top'
                            }
                        }, itemStyle: {
                            normal: { color: '#000' },
                        }
                    });

                const a1 = new Array();
                a1.push(e[5].c2);
                a1.push(e[5].c3);
                a1.push(e[5].c4);
                a1.push(e[5].c5);
                a1.push(e[5].c6);
                a1.push(e[5].c7);
                a1.push(e[5].c8);
                a1.push(e[5].c9);
                a1.push(e[5].c10);
                a1.push(e[5].c11);
                a1.push(e[5].c12);

                const color = this.getColor(e[0].c2);
                beas2.push({
                    data: a1, type: 'bar', name: e[0].c4 + '_' + e[0].c3 + '吃饭/休息', label: {
                        normal: {
                            show: true,
                            position: 'top'
                        }
                    }, itemStyle: {
                        normal: { color: color },
                    }
                });
                a = new Array();
                a.push(e[2].c2);
                a.push(e[2].c3);
                a.push(e[2].c4);
                a.push(e[2].c5);
                a.push(e[2].c6);
                a.push(e[2].c7);
                a.push(e[2].c8);
                a.push(e[2].c9);
                a.push(e[2].c10);
                a.push(e[2].c11);
                a.push(e[2].c12);
                beas2.push({
                    data: a, type: 'line', name: e[0].c4 + '_' + e[0].c3, label: {
                        normal: {
                            show: true,
                            position: 'top'
                        }
                    }, itemStyle: {
                        normal: { color: color },
                    }
                });
            } else if (e[0].c2 === '3班') {
                a = new Array();
                a.push(e[3].c2);
                a.push(e[3].c3);
                a.push(e[3].c4);
                a.push(e[3].c5);
                a.push(e[3].c6);
                a.push(e[3].c7);
                a.push(e[3].c8);
                a.push(e[3].c9);
                a.push(e[3].c10);
                a.push(e[3].c11);
                a.push(e[3].c12);
                beas3.push({
                    data: a, type: 'line', name: '目标JPH', label: {
                        normal: {
                            show: true,
                            position: 'top'
                        }
                    }, itemStyle: {
                        normal: { color: '#000' },
                    }
                }, {
                        data: this.getGrossJph(3), type: 'line', name: 'GrossJPH', label: {
                            normal: {
                                show: true,
                                position: 'top'
                            }
                        }, itemStyle: {
                            normal: { color: '#000' },
                        }
                    });

                const a1 = new Array();
                a1.push(e[5].c2);
                a1.push(e[5].c3);
                a1.push(e[5].c4);
                a1.push(e[5].c5);
                a1.push(e[5].c6);
                a1.push(e[5].c7);
                a1.push(e[5].c8);
                a1.push(e[5].c9);
                a1.push(e[5].c10);
                a1.push(e[5].c11);
                a1.push(e[5].c12);
                const color = this.getColor(e[0].c2);
                beas3.push({
                    data: a1, type: 'bar', name: e[0].c4 + '_' + e[0].c3 + '吃饭/休息', label: {
                        normal: {
                            show: true,
                            position: 'top'
                        }
                    }, itemStyle: {
                        normal: { color: color },
                    }
                });
                a = new Array();
                a.push(e[2].c2);
                a.push(e[2].c3);
                a.push(e[2].c4);
                a.push(e[2].c5);
                a.push(e[2].c6);
                a.push(e[2].c7);
                a.push(e[2].c8);
                a.push(e[2].c9);
                a.push(e[2].c10);
                a.push(e[2].c11);
                a.push(e[2].c12);
                beas3.push({
                    data: a, type: 'line', name: e[0].c4 + '_' + e[0].c3, label: {
                        normal: {
                            show: true,
                            position: 'top'
                        }
                    }, itemStyle: {
                        normal: { color: color },
                    }
                });
            }
        });

        const temp = new Array();
        for (let i = 0; i < legend.length; i++) {
            temp.push(legend[i] + '吃饭/休息');
        }
        temp.forEach(e => legend.push(e));

        if (beas1.length > 0) {
            this.isShowCharts1 = true;
            this.chartOption1 = this.createOption(beas1, legend, 1);
        }
        if (beas2.length > 0) {
            this.isShowCharts2 = true;
            this.chartOption2 = this.createOption(beas2, legend, 2);
        }
        if (beas3.length > 0) {
            this.isShowCharts3 = true;
            this.chartOption3 = this.createOption(beas3, legend, 3);
        }

        if (this.isShowCharts3 === false && this.isShowCharts2 === false && this.isShowCharts1 === false) {
            this.isShowCharts1 = true;
        }

    }

    createOption(datas: any, legend: any, shift: number) {
        const x = [];
        if (shift === 1) {
            for (let i = 1; i <= this.workNum1; i++) {
                x.push(i);
            }
        }
        if (shift === 2) {
            for (let i = 1; i <= this.workNum2; i++) {
                x.push(i);
            }
        }
        if (shift === 3) {
            for (let i = 1; i <= this.workNum3; i++) {
                x.push(i);
            }
        }

        return {
            // title: {
            //     text: '折线图堆叠'
            // },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data: legend
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: x
            },
            yAxis: {
                type: 'value'
            },
            series: datas
        };
    }


    // tslint:disable-next-line:member-ordering
    colorMap = new Map<any, number>();
    getColor(shiftVal0: any) {
        if (!this.colorMap.has(shiftVal0)) {
            this.colorMap.set(shiftVal0, 0);
        }
        let index = this.colorMap.get(shiftVal0);
        let c;
        if (index === 0) {
            c = '#0070C0';
        } else if (index === 1) {
            c = '#FFC000';
        } else if (index === 2) {
            c = '#00B050';
        } else {
            c = this.getRandomColor();
        }
        index += 1;
        this.colorMap.set(shiftVal0, index);
        return c;

    }


    getRandomColor() {
        const colorValue = '0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f';
        const colorArray = colorValue.split(',');
        let color = '#';
        for (let i = 0; i < 6; i++) {
            color += colorArray[Math.floor(Math.random() * 16)];
        }
        return color;
    }

    onExporting(e) {
        e.fileName = e.fileName + new Date().toISOString().replace(/[\-\:\.]/g, '').split('T')[0];
    }
}
